package com.example.translateappjava.Activity;

import android.content.SharedPreferences;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.View;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;
import com.example.translateappjava.R;

public class ThemeUtilsActivity {

    private static final int DEFAULT_COLOR = R.color.color1_6;

    public static void applyTheme(View rootView) {
        if (rootView == null) {
            Log.e("utils", "rootview == null");
        } else {
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
        }
        applyColorSettings(rootView);
        applyBackgroundSettings(rootView);
        applyOpacitySetting(rootView); // 新增透明度设置
    }

    public static void applyColorSettings(View rootView) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(rootView.getContext());
        int defaultColor = rootView.getResources().getColor(DEFAULT_COLOR);
        int color = sharedPreferences.getInt("color", defaultColor);
        Toolbar toolbar = rootView.findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(color);
        // 如果有其他需要应用颜色的视图，可以在这里设置
    }

    public static void applyBackgroundSettings(View rootView) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(rootView.getContext());
        String defaultBackground = "white_solid"; // 默认背景
        String background = sharedPreferences.getString("background", defaultBackground);

        System.out.println(background + "~~~~~~~~~~~~~~~~~~");
        switch (background) {
            case "green_gradient":
                applyGradientBackground(new int[]{0xFFE7F0C3, 0xFFA7D1AC, 0xFF3DABAA, 0xFF577982, 0xFF5A5152}, rootView);
                break;
            case "blue_gradient":
                applyGradientBackground(new int[]{0xFFA1CAFF, 0xFF6A95CC, 0xFF3A4D8F, 0xFF132666, 0xFF332B3A}, rootView);
                break;
            case "dusk_gradient":
                applyGradientBackground(new int[]{0xFFF8B195, 0xFFF67280, 0xFFC06C84, 0xFF6C5B7B, 0xFF355C7D}, rootView);
                break;
            case "white_solid":
                applySolidBackground(0xFFF1F1F5, rootView);
                break;
        }
    }

    private static void applyGradientBackground(int[] colors, View view) {
        GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
        Log.e("ThemeUtilsActivity", "view++++++++++++++++++++++++++++");
        if (view != null) {
            Log.e("ThemeUtilsActivity", "view666666666666666666666666");
            view.setBackground(gradientDrawable);
        }
    }

    private static void applySolidBackground(int color, View view) {
        view.setBackgroundColor(color);
    }

    // 新增透明度设置方法
    public static void applyOpacitySetting(View rootView) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(rootView.getContext());
        int savedOpacity = sharedPreferences.getInt("background_opacity", 25); // 默认全不透明

        // 确保 rootView 有背景
        if (rootView.getBackground() != null) {
            rootView.getBackground().setAlpha(savedOpacity);
        } else {
            // 如果没有背景，设置一个默认背景，然后设置透明度
            applySolidBackground(0xFFF1F1F5, rootView);
            rootView.getBackground().setAlpha(savedOpacity);
        }
    }
}
